package cn.micai.util;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * 描述：
 * <p>
 *
 * @author: 赵新国
 * @date: 2018/3/30 16:18
 */
public class TreeNodeTest {

    /**
     * 根节点
     */
    private TreeNode rootNode = new TreeNode();

    /**
     * 关键词编码
     */
    private String charset = "utf-8";

    /**
     * 创建DFA
     * @param keywordList
     * @throws UnsupportedEncodingException
     */
    public void createKeywordTree(List<String> keywordList) throws UnsupportedEncodingException{
        for (String keyword : keywordList) {
            if(keyword == null) continue;
            keyword = keyword.trim();
            byte[] bytes = keyword.getBytes(charset);
            TreeNode tempNode = rootNode;
            for (int i = 0; i < bytes.length; i++) {
                int index = bytes[i] & 0xff;
                TreeNode node = tempNode.getSubNode(index);
                if(node == null){
                    node = new TreeNode();
                    tempNode.setSubNode(index, node);
                }
                tempNode = node;
                if(i == bytes.length - 1){
                    tempNode.setKeywordEnd(true);
                }
            }
        }
    }


    public String searchKeyword(String text) throws UnsupportedEncodingException{
        return searchKeyword(text.getBytes(charset));
    }

    public String searchKeyword(byte[] bytes) throws UnsupportedEncodingException {
        StringBuilder words = new StringBuilder();
        if(bytes == null || bytes.length == 0){
            return words.toString();
        }
        TreeNode tempNode = rootNode;
        int rollback = 0;
        int position = 0;

        while (position < bytes.length) {
            int index = bytes[position] & 0xFF;

            tempNode = tempNode.getSubNode(index);
            if(tempNode == null){
                position = position - rollback;
                rollback = 0;
                tempNode = rootNode;

            }
            else if(tempNode.isKeywordEnd()){


                byte[] temp = new byte[rollback+1];
                int j = 0;
                for (int i = position-rollback; i <= position; i++,j++) {
                    temp[j] = bytes[i];
                }
                String tempString = new String(temp,charset);

                int index1 = 0;
                int length = tempString.length();
                for (int i = 0; i <= rollback; i++) {
                    if(index1<length)
                        bytes[position-i] = 42;
                    else
                        bytes[position - i] = 0;
                    index1++;
                }

                rollback = 1;
            }else{
                rollback++;
            }
            position++;
        }
        int leng = 0;
        for(int i = 0; i < bytes.length; i++) {
            if(bytes[i] != 0)
                leng ++;
        }
        byte[] bytes2 = new byte[leng];
        int indexB2 = leng-1;

        for(int i = bytes.length-1; i >= 0; i--) {
            if(bytes[i] != 0) {
                bytes2[indexB2] = bytes[i];
                indexB2--;
            }
        }
        String result = null;
        try {
            result  =  new String(bytes2,"utf-8");

        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        String[] arr = {"习近平", "法轮功", "法轮", "习近"};
        String content = "习近平反共多少分开了家里打扫房间丽舍大街路口反抗螺丝钉警方立刻圣诞节快乐房里看电视克利夫兰科达斯科拉菲快乐圣诞节发卡量达斯科拉反法轮功G该罚的辅导感到十分个地方鬼地方感动发给地方鬼地方鬼地方鬼地方感到十分鬼地方感到十分鬼地方感动中共地方鬼地方感到十分感到十法轮分鬼地方广东省打手犯规大塞弗鬼地方感到十分古典风格对方是个共产梵蒂冈地方感习近动独立分开计算的离开家连锁店接连发生的离开分快乐圣诞节分卡洛斯的离开离开家得了三等奖路口发生的离开洗脑辅导告诉对方感到十分感动发给三等份鬼地方感到十分鬼地方鬼地方地方GCD鬼地方梵蒂冈地方该罚的鬼地方鬼地方鬼地方鬼地方给对方是个打手犯规第四个习近 CCP地方鬼地方感动大放送感到十分感到十分光的反射感到十分告诉对方感到十分广东省GONG党 豆腐干大塞弗感到十分告诉对方告诉对方感到十分感到十分敢死队大塞弗网特 梵蒂冈打手犯规打手犯规对方是个大放送给对方是个地方暴政 梵蒂冈地方广东省反攻倒算豆腐干对方是个豆腐干大放送";
        TreeNodeTest t = new TreeNodeTest();
        List<String> words = new ArrayList<String>();
        for (String in : arr) {
            words.add(in);
        }
        t.createKeywordTree(words);
        String result = t.searchKeyword(content);
        System.out.println(result);
    }

}
